//define macro listed below
//
//#define __StrChkPermission(x)
//#define __GetNextBlk(x)
//#define __StrWritingAddr(x)
//#define __BlkStateAddr(strType stream)
//#define __BlkWrittenAmount(strType stream)

//#define perSize
//#define strType

//Macro of both Stream & Muti-Stream


#include <structure/list.h>
#include <BasicMatic.h>
#include <MC.h>


#define HIGHB(n)			(0xFFFFFFFF << (32 - n))
#define LOWB(n)				(0xFFFFFFFF >> (32 - n))
#define HWREG(x)                        (*((volatile unsigned int *)(x)))

#define u8				unsigned char
#define u16				unsigned short
#define	u32				unsigned int

#define Blk_ReadingMax			31
#define IS_STREAM			(1 << 31)
#define	IS_FULL				(1 << 30)
#define IS_END				(1 << 29)
#define IS_JUMPING			(1 << 28)
//#define IS_SYSTEM			(1 << 27)
#define IS_INUSE			(1 << 27)			

#define __BlkState(x)			(HWREG(__BlkStateAddr(x)))
#define WRITTEN_AMOUNT(x)		((((u32)(x)) & (LOWB(16))))
#define WRITING_POINT(x)		((((u32)(x)) & (LOWB(16))))
#define READING_POINT(x)		((((u32)(x)) & (LOWB(16))))
#define JUMPING_TO(x)			(WRITTEN_AMOUNT(x))
#define ALL_READING(x)			(((x) >> 16) & LOWB(5))
#define NOW_READING(x)			(((x) >> 21) & LOWB(5))
#define ALL_READING_CLEAR(x)		do{__BlkState(x) &= (~(LOWB(4) << 16));}while(0);  //x is vaddr
#define NOW_READING_CLEAR(x)		do{__BlkState(x) &= (~(LOWB(4) << 21));}while(0);  

#define ALL_READING_ADD(x)		((ALL_READING(__BlkState(x)) == Blk_ReadingMax)?0:((__BlkState(x) += (1 << 16)),1))

#define NOW_READING_ADD(x)		((NOW_READING(__BlkState(x)) == Blk_ReadingMax)?0:((__BlkState(x) += (1 << 21)),1))

#define NOW_READING_MINUS(x)		((NOW_READING(__BlkState(x)) == 0)?0:((__BlkState(x) -= (1 << 21)),1))

#define SET_STREAM_WRITTEN_AMOUNT(x,y)	do								\
{													\
unsigned int* STREAM_2198784989487 = __BlkStateAddr(x);					\
HWREG(STREAM_2198784989487) &= ~LOWB(16);									\
HWREG(STREAM_2198784989487) |= ((u32)(y));										\
}while(0);												
//x is the _str, y is the amount

#define SET_STREAM_JUMPING_TO(x,y)	SET_STREAM_WRITTEN_AMOUNT((x),(y))

#define EndStream(x)		do{__BlkState((x)) |= IS_END;}while(0);

//x is the _str, y is the next address

#define __GetLastBlk(stream)		\
(					\
{while(__BlkState(stream) & IS_JUMPING)	\
	{stream = _GetNextBlk(stream);}	\
	stream;				\
}					\
)					




